var net = require('net');
//var Zredis = require('./zredis.js');

var PORT = 502;
var HOST = '127.0.0.1';
var KEY = 'key-data';

var TCPcmd = '000100000006010300000006';
var RTUcmd = '010300000006';

// tcp客户端
var client = net.createConnection(PORT, HOST);
//var redis = Zredis();
client.on('connect', function(){
    console.log('client: on connect ');
	
	
});

client.on('data', function(data){
    //console.log('client:on data {'+ data +'}');
	
	var buf = Buffer(data,'hex');
	console.log(buf);
	if(buf.length >=20){
		var getdata ={};
		getdata.wendu=''+buf.readInt16BE(9);
		getdata.shidu=''+buf.readInt16BE(11);
		getdata.dianya=''+buf.readInt16BE(13);
		getdata.dianliu=''+buf.readInt16BE(15);
		getdata.gonglv=''+buf.readInt16BE(17);
		getdata.dianneng=''+buf.readInt16BE(19);
		/*	
		redis.SetData(KEY,getdata,function(err,res){
		
		if(err){
			console.log('SetData err:'+err);
		}
		
	});*/
		
	}else {
		
		console.log('buf:'+buf);
		
	}

	
	
	
});

client.on('close', function(data){
    console.log('client: on close');
});

client.on('erro', function(erro){
    console.log('client: on erro');
});

setInterval(function(){
	
	//console.log('clients.ws.readyState : '+client.readyState );
	if(client.readyState === 'open' ||client.readyState === 'OPEN' ){	
		var buffer = Buffer(TCPcmd,'hex');
		console.log(buffer);
		client.write(buffer);
		
	}else {
		
		//onsole.log('clients.ws.readyState : '+client.readyState );
		
	}

},500);



client.on("error",function(err){
            console.log("client: on err",err);
});

//client.end('hello, client');